package com.iweb.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import com.iweb.commons.ResultEntity;
import com.iweb.model.Game;
import com.iweb.model.OrderG;
import com.iweb.service.GameService;
import com.iweb.service.OrderGservice;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Time :2022/8/16 8:40
 * Author : kriller
 * Project : steam-malls
 */
@RestController
@RequestMapping("order")
public class OrderGController {
    @Resource
    private OrderGservice orderGservice;

    @Resource
    private GameService gameService;


    //创建订单
    @PostMapping("init")
    public ResultEntity<OrderG> orderGInit(@RequestParam Integer gameId){
        //Integer userId = getUserId();
        Integer userId = 1;
        OrderG orderG = new OrderG();
        orderG.setOrderGCreate(new Date());
        Game game = gameService.selectById(gameId);
        orderG.setOrderGPrice(game.getGamePrice());
        orderG.setOrderGState(0);
        orderG.setGameId(gameId);
        orderG.setGameName(game.getGameName());
        orderG.setUserId(userId);
        orderG.setOrderGId(IdUtil.getSnowflake(1, 2).nextId());
        orderGservice.insertNewOrder(orderG);
        return  new ResultEntity<>(2000,"生成订单",true,orderG);
    }
    //支付订单,更改订单状态
    @PostMapping("pay")
    public ResultEntity<OrderG> payByOrderGId(@RequestParam Long orderGId){

        orderGservice.updateStateForPayById(orderGId);

        return new ResultEntity<>(2000,"支付订单",true,null);
    }
    //退款
    @PostMapping("refund")
    public ResultEntity<OrderG> refundOrderG(@RequestParam Long orderGId){
        System.out.println(orderGId);
        orderGservice.updateStateForRefundById(orderGId);
        return new ResultEntity<>(2000,"退款成功",true,null);
    }

    //获取用户的所有订单
    @GetMapping("user_order")
    public ResultEntity<List<OrderG>> listUserOrderG(){
//        Integer userId = getUserId();
        Integer userId = 1;
        List<OrderG> orderGS  = orderGservice.listUserOrderG(userId);
        for (OrderG orderG : orderGS) {
            orderG.setOrderGIdString(orderG.getOrderGId().toString());
            System.out.println(orderG.getOrderGIdString());
        }
        System.out.println(1);
        return new ResultEntity<>(2000,"用户的所有订单",true,orderGS);
    }

    private Integer getUserId() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        return Integer.parseInt(JWT.of(token).getPayload("userId").toString());

    }
}
